/**
 * Package:com.sofmit.component.bill.util;
 * $Id: DateUtil.java,v 1.3 2009/06/02 04:23:06 yangc Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;

/**
 * DateUtil
 * <p/>
 * <p><a href="DateUtil.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: yangc $</a>
 * @version $Revision: 1.3 $
 */
public class DateUtil {
    public final static int DATE_FORMAT_LONG=0;
    public  final static int DATE_FORMAT_MIDDLE=1;
    public  final static int DATE_FORMAT_SHORT=2;
    public  final static int DATE_FORMAT_SIMPLE=3;
    public final static String DATE_FORMAT_PATTEN_LONG="yyyy-MM-dd HH:mm:ss";
    public  final static String DATE_FORMAT_PATTEN_MIDDLE="yyyy-MM-dd";
    public  final static String DATE_FORMAT_PATTEN_SHORT="yyyy-M-d";
    public  final static String DATE_FORMAT_PATTEN_SIMPLE="yyyyMMdd";

    /**
     * 根据当前时间戳,返回当前的时间字符串
     * @param time
     * @param model
     * @return
     */
    public static String friendDate(long time, int model) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat();
        if (model == DATE_FORMAT_LONG) bartDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTEN_LONG);
        if (model == DATE_FORMAT_MIDDLE) bartDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTEN_MIDDLE);
        if (model == DATE_FORMAT_SHORT) bartDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTEN_SHORT);
        return bartDateFormat.format(new Date(time));
    }

    /**
     * 返回当前时间对象
     * @return
     */
    public static Date getCurrentDate() {
        return new Date();
    }

    /**
     * 取得当前时间串的便利方法,返回格式为:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String friendDate() {
         return friendDate(new Date().getTime(),DATE_FORMAT_LONG);
    }

    /**
     * 取得当前时间串的便利方法,返回格式根据参数而定
     * @param model
     * @return
     */
    public static String friendDate(int model) {
         return friendDate(new Date().getTime(),model);
    }

    /**
     * 将指定时间格式为字符串,返回格式为:yyyy-MM-dd
     * @param date
     * @return
     */
     public static String dateToString(Date date) {
       SimpleDateFormat bartDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTEN_MIDDLE);
        return bartDateFormat.format(date);
     }
    /**
     * 将当前时间格式为字符串,返回格式为:yyyy-MM-dd
     * @return
     */
      public static String dateToString() {
       SimpleDateFormat bartDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTEN_MIDDLE);
        return bartDateFormat.format(new Date());
     }
    /**
     * 将当前时间格式为字符串,返回格式为:yyyyMMdd
     * @return
     */
      public static String dateToStringShort() {
       SimpleDateFormat bartDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTEN_SIMPLE);
        return bartDateFormat.format(new Date());
     }

    /**
     * 将字符串解析为Date对象,按照常用的日期字符串格式进行解析,如果解析失败,返回空
     * @param dateString
     * @return
     */
    public static Date parseDate(String dateString) {
       if(parseDate(DATE_FORMAT_PATTEN_MIDDLE,dateString)!=null)return parseDate(DATE_FORMAT_PATTEN_MIDDLE,dateString);
       if(parseDate(DATE_FORMAT_PATTEN_LONG,dateString)!=null)return parseDate(DATE_FORMAT_PATTEN_LONG,dateString);
       if(parseDate(DATE_FORMAT_PATTEN_SHORT,dateString)!=null)return parseDate(DATE_FORMAT_PATTEN_LONG,dateString);
       if(parseDate(DATE_FORMAT_PATTEN_SIMPLE,dateString)!=null)return parseDate(DATE_FORMAT_PATTEN_SIMPLE,dateString);
        return null;
    }

    /**
     * 将指定的时间字符串解析为unix时间戳
     * @param dateString
     * @return
     */
    public static Long parseDateToLong(String dateString) {
        return parseDate(dateString).getTime();
    }

    /**
     * 将指定的时间字符串解析为unix时间戳
     * @param pattern 匹配模式
     * @param dateString 要解析的字符串
     * @return
     */
    public static Long parseDateToLong(String pattern,String dateString) {
        return parseDate(pattern,dateString).getTime();
    }
    /**
     * 将指定的时间字符串解析为Date对象
     * @param pattern 匹配模式
     * @param dateString 要解析的字符串
     * @return
     */

    public static Date parseDate(String pattern, String dateString) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(dateString);
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据生日计算年龄
     * @param birthday
     * @return
     */
    public static Integer calculateAge(Date birthday){
        if(birthday == null)return 0;
        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
        Calendar c = Calendar.getInstance();
                c.setTime(birthday);
        int nowYear = c.get(Calendar.YEAR);
        return nowYear-thisYear;
    }

    /**
     * 根据生日的时间戳计算年龄
     * @param birthday
     * @return
     */
    public static Integer calculateAge(Long birthday){
        if(birthday == null || birthday <= 0)return 0;
        return calculateAge(new Date(birthday));
    }



    public static void main(String[] args) {
        System.out.println("LONG:" + (DateUtil.parseDateToLong("yyyy-MM-dd HH:mm:ss", "2008-01-01 00:00:00")/1000)%(24*60*60));
        long pre=0;
        for(int i=0; i<24;i=i+1){
            long mo = (DateUtil.calculateHour(DateUtil.parseDate("yyyy-MM-dd HH:mm:ss","2008-01-01 00:00:00"),i).getTime()/1000)%(24*60*60);
            System.out.println("hour:"+i+":" + mo);
            System.out.println("c-p="+ (mo-pre));
            pre=mo;
        }
        System.out.println("LONG:" + (DateUtil.parseDateToLong("yyyy-MM-dd HH:mm:ss", "2008-01-01 22:00:00")/1000)%(24*60*60));
        long mo = (DateUtil.parseDateToLong("yyyy-MM-dd HH:mm:ss", "2008-01-01 03:16:23")/1000)%(24*60*60);
        System.out.println("mo:"+mo);
        System.out.println("hour:"+((mo/7200+4)%12+1));

    }


    public static Date calculateDay(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(GregorianCalendar.DATE, days);
        return cal.getTime();
    }

    public static Date calculateHour(Date date, int hours) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(GregorianCalendar.HOUR, hours);
        return cal.getTime();
    }
    public static Date calculateMin(Date date, int Min) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(GregorianCalendar.MINUTE, Min);
        return cal.getTime();
    }
    
    public static long getDayCount(long time){
    	return time/1000/60/60/24;
    }
    
    public static long getHourCount(long time){
    	time = time%(1000*60*60*24);
    	return time/1000/60/60;
    }
    
    public static long getMinCount(long time){
    	time = time%(1000*60*60);
    	return time/1000/60;
    }
    
    public static long getSecCount(long time){
    	time = time%(1000*60);
    	return time/1000;
    }

}
